<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Comment_Services_Comment
{
	public static function validate($data)
	{
		$validator = new Gio_Core_Validator();
		foreach ($data as $index => $value) {
			if (gettype($value) == 'string') {
				$data[$index] = trim($value);
			}
		} 
		
		$data['messages']['title'] = $validator->setString($data['title'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'article_title_notnull',
																)
														);
		
		$data['messages']['slug'] = $validator->setString($data['slug'])
												->validate(array('notNull', 'isSlug')
																, array(
																	'notNull' => 'article_slug_notnull',
																	'notNull' => 'article_slug_invalid',
																)
														);
		
		$descriptionTemp = Gio_Core_String::stripTags($data['description'], null);
		$data['messages']['description'] = $validator->setString($descriptionTemp)
												->validate(array('notNull')
																, array(
																	'notNull' => 'article_description_notnull',
																)
														);
														
		$contentTemp = Gio_Core_String::stripTags($data['content'], null);
		$data['messages']['content'] = $validator->setString($contentTemp)
												->validate(array('notNull')
																, array(
																	'notNull' => 'article_content_notnull',
																)
														);
														
		$data['messages']['category_id'] = $validator->setString($data['category_id'])
												->validate(array('isNumeric')
																, array(
																	'isNumeric' => 'article_category_id_invalid',
																)
														);
		
		$error = false;
		foreach ($data['messages'] as $index => $message) {
			if ($message) {
				$data['messages_error'] = true;
				return $data;
			}
		}										
		$data['messages_error'] = false;
		return $data; 
	}
	
	public static function getById($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->getById($id);
	}
	
	public static function activate($comment)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->update($comment);
	}
	
	public static function add($comment)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->add($comment);
	}
	
	public static function reupdateOrderInThread($comment)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->reupdateOrderInThread($comment);
	}
	
	public static function getThreadComments($offset, $count, $params, $isActive = null)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->getThreadComments($offset, $count, $params, $isActive);
	}
	
	public static function countThreadComments($params, $isActive = null)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->countThreadComments($params, $isActive);
	}
	
	public static function find($offset = null, $limit = null, $condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->find($offset, $limit, $condition);
	}
	
	public static function count($condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Comment_Models_Mysql_Comment();
		$dao->setConnection($conn);
		return $dao->count($condition);
	}
}